package com.googlecode.cswish.struts.conversion;

import java.text.DecimalFormat;
import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

public class DoubleConverter extends StrutsTypeConverter {

	@Override
	public Object convertFromString(Map context, String[] values, Class toClass) {
		Double val = null;
		if (values != null && values.length > 0) {
			String value = values[0];
			if (value.length() > 0) {				
				val = new Double(values[0]);
			} else {
				val = 0d;
			}
		}
		return val;
	}

	@Override
	public String convertToString(Map context, Object o) {
		String val;
		if (o != null) {
			DecimalFormat df = new DecimalFormat("0.######"); 
			val = df.format(o); 
		} else {
			val = null;
		}
		return val;
	}
}
